package littlecyberwar.ui;

import javax.swing.JPanel;
import javax.swing.JScrollPane;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

import littlecyberwar.tool.MouseHandler;
import littlecyberwar.util.Util;

//the control panel is placed on the right side of the playing surface
//the ControlPanel is made up of 4 JPanels: Starting from top:
//
//1. UnitFrontView (will later include unit stats window)
//2. ProfilePanelCard ( a card layout of all possible unit profiles)
//3. ModePanel (a grid of control mode button group)
//4. GameActionPanel (game specific dialog and gameplay controls)
//5. STatus Label
//6. MiniMapView

public class ControlPanel extends JPanel {
  JPanel modePanel; // for all the modal tools
  JPanel toolPanel;  // for all the onbe shot tools

  public static final int numOfControlButtonRow = 3;
  public static final int numOfControlButtonCol= 3;
  public static final int numOftoolButtonRow= 1;
  public final static int _dividerSize=2;


  private PlayingBoardPanel board;
  private MiniMapPanel miniMap;
  ButtonGroup commandModeBG;

  private JLabel sideView;
  private Dimension sideViewDimension = new Dimension(240, 180);	// add the text height as well
  public static ImageIcon sideViewImage = new ImageIcon(littlecyberwar.ui.MainFrame.class.getResource("wallpap1.gif"));
  
  LCWButton but1;
  LCWButton but2;
  LCWButton but3;
  LCWButton but4;
  LCWButton but5;
  LCWButton but6;
  LCWButton but7;
  LCWButton but8;
  LCWButton but9;
  LCWButton but10;
  LCWToolButton but11;
  LCWButton but12;
  LCWButton butWatch;
  LCWButton butAlignHorizontal;
  LCWButton butAlignFacing;
  LCWButton butAlignVertical;
  LCWButton butRemove;
  LCWButton butChangeLayer;
  LCWButton butReset;
  LCWToolButton loadBut;
  LCWToolButton recordBut;
  LCWToolButton	stopBut;
  LCWToolButton playBut;
  
  // a panel that's a cardlayout of all the unit profiles there is
  static ProfileCardPanel profilePanel;
  // static CardLayout profilePanelCard;
  
  /**
   * Create all the component parts
   * Make sure the sideView is created first before the Mode Panel since side view
   * is needed for each of the mouseHandler.
   */
  public ControlPanel(PlayingBoardPanel bIn) {
    board = bIn;
    initSideView();
	profilePanel = new ProfileCardPanel();
    initModePanel();
    initToolPanel();

    board.setSideViewLabel( sideView );

    // add all children to the component
    this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
	sideView.setAlignmentX(JComponent.CENTER_ALIGNMENT);
    this.add(sideView);
	profilePanel.setAlignmentX(JComponent.CENTER_ALIGNMENT);
	JScrollPane scrollProfile = new JScrollPane(profilePanel, 
			JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    this.add(scrollProfile);
    
    /**
     * Add glue between scrollProfile and mode buttons 
     */
    this.add(Box.createVerticalGlue());
    
	modePanel.setAlignmentX(JComponent.CENTER_ALIGNMENT);
    this.add(modePanel);
	toolPanel.setAlignmentX(JComponent.CENTER_ALIGNMENT);
    this.add(toolPanel);
    
    // this.add(Box.createRigidArea(new Dimension(0,5)));
  }

  private void initSideView() {
    // setup default side view stuff
    // setup sideView
    sideView = new JLabel();
    sideView.setIcon(sideViewImage);
    // ! Worry about size of picture later
    sideView.setPreferredSize(sideViewDimension);
    sideView.setHorizontalTextPosition(JLabel.CENTER);
	sideView.setVerticalTextPosition(JLabel.BOTTOM);
    sideView.setAlignmentX(Component.CENTER_ALIGNMENT);
	sideView.setIconTextGap(0);
    Border b = BorderFactory.createLoweredBevelBorder();
    sideView.setBorder(b);
    board.setSideViewLabel(sideView);
    return;
  }
  
  private void initModePanel() {
    modePanel = new JPanel();

    //!! Need to have a scheme to calculate the size of component depending on number of buttons present
    Dimension d = new Dimension(MiniMapPanel._setMiniMapWidth,
    numOfControlButtonRow*(LCWButton._buttonSize+_dividerSize)+_dividerSize);
    modePanel.setPreferredSize(d);
    modePanel.setMinimumSize(d);
    modePanel.setMaximumSize(d);

    modePanel.setLayout(new FlowLayout(FlowLayout.CENTER, _dividerSize,_dividerSize));

    but1 = new LCWButton("MouseRotateLeftHandler", board);    
    but2 = new LCWButton("MouseStraightForwardHandler", board);
    but3 = new LCWButton("MouseBackwardHandler", board);
    but4 = new LCWButton("MouseRotateRightHandler", board);
    but5 = new LCWButton("MouseSidestepHandler", board);
    but6 = new LCWButton("MouseObliqueHandler", board);
    but7 = new LCWButton("MouseFreeFormHandler", board);
    but8 = new LCWButton("MouseRulerHandler", board);
    but9 = new LCWButton("MouseRotateCenterHandler", board);
    but10 = new LCWButton("MousePlacementHandler", board);
	but12 = new LCWButton(null, board);
	butWatch = new LCWButton("MouseWatchHandler", board);
	butAlignHorizontal = new LCWButton("MouseAlignHorizontalHandler", board);
	// butAlignFacing =  new LCWButton("MouseAlignFacingHandler", board);
	butAlignVertical =  new LCWButton("MouseAlignVerticalHandler", board);
	butRemove =  new LCWButton("MouseRemoveHandler", board);
	butChangeLayer =  new LCWButton("ChangeLayerHandler", board);
	butReset = new LCWButton(null, board);

    // put all the command buttons into a button group
    commandModeBG = new ButtonGroup();
    commandModeBG.add(but1);
    commandModeBG.add(but2);
    commandModeBG.add(but4);
	commandModeBG.add(but3);
    commandModeBG.add(but5);
    commandModeBG.add(but6);
    commandModeBG.add(but7);
    commandModeBG.add(but8);
    commandModeBG.add(but9);
    commandModeBG.add(but10);
	commandModeBG.add(butWatch);
	// commandModeBG.add(butAlignFacing);
	commandModeBG.add(butAlignVertical);
	commandModeBG.add(butAlignHorizontal);
	commandModeBG.add(butRemove);
	commandModeBG.add(butChangeLayer);
	commandModeBG.add(butReset);
    
    // some are NOT in the button group
    // like button 12 (the chat button)

    // put all command buttons onto the modePanel
	modePanel.add(butWatch);
    modePanel.add(but1);
    modePanel.add(but2);
    modePanel.add(but4);
	modePanel.add(but3);
	modePanel.add(but5);
    modePanel.add(but6);
    modePanel.add(but7);
    modePanel.add(but8);
    modePanel.add(but9);
    modePanel.add(but10);
	modePanel.add(but12);
	// modePanel.add(butAlignFacing);
	modePanel.add(butAlignVertical);
	modePanel.add(butAlignHorizontal);
	modePanel.add(butRemove);
	modePanel.add(butChangeLayer);
	modePanel.add(butReset);
    return;
  }
  
  // Due to ordering problems, this must be called after the actions
  // are initialized
  //
  public void associateActionsToButtons() {
	LCWAction action= (LCWAction)Util.lookup.get(Util._leftKey);    
	but1.setAction(action);
	
	action= (LCWAction)Util.lookup.get(Util._forwardKey);
	but2.setAction(action);
	LCWAction ma = action;

	action= (LCWAction)Util.lookup.get(Util._backKey);
	but3.setAction(action);

	action= (LCWAction)Util.lookup.get(Util._rightKey);
	but4.setAction(action);

	action= (LCWAction)Util.lookup.get(Util._sideKey);
	but5.setAction(action);

	action= (LCWAction)Util.lookup.get(Util._obliqueKey);
	but6.setAction(action);

	action= (LCWAction)Util.lookup.get(Util._freeKey);
	but7.setAction(action);

	action= (LCWAction)Util.lookup.get(Util._tapeKey);
	but8.setAction(action);

	action= (LCWAction)Util.lookup.get(Util._rotateKey);
	but9.setAction(action);

	action= (LCWAction)Util.lookup.get(Util._deployKey);
	but10.setAction(action);

	action= (LCWAction)Util.lookup.get(Util._diceKey);
	but11.setAction(action);
	
	action= (LCWAction)Util.lookup.get(Util._chatKey);
	but12.setAction(action);
	
	action= (LCWAction)Util.lookup.get(Util._recordKey);
	recordBut.setAction(action);
	
	action= (LCWAction)Util.lookup.get(Util._loadKey);
	loadBut.setAction(action);
	
	action= (LCWAction)Util.lookup.get(Util._stopKey);
	stopBut.setAction(action);
	
	action= (LCWAction)Util.lookup.get(Util._playKey);
	playBut.setAction(action);
	
	action= (LCWAction)Util.lookup.get(Util._watchKey);
	butWatch.setAction(action);

	action= (LCWAction)Util.lookup.get(Util._alignHorizontalKey);
	butAlignHorizontal.setAction(action);
	
	action= (LCWAction)Util.lookup.get(Util._alignVerticalKey);
	butAlignVertical.setAction(action);
	
	action= (LCWAction)Util.lookup.get(Util._removeKey);
	butRemove.setAction(action);
	
	action= (LCWAction)Util.lookup.get(Util._changeLayerKey);
	butChangeLayer.setAction(action);
	
	action= (LCWAction)Util.lookup.get(Util._resetMoveKey);
	butReset.setAction(action);
	
	
	// this has to match the default action selected in ControlPanel
	// Initialize the default first mouse handler to straight ahead?
	//
	MouseHandler h = (MouseHandler)ma.getValue(ModeAction.LCW_MOUSEHANDLER);
	board.setMouseHandler(h);
	Util.lookup.put(Util._CURR_MOUSE_HANDLER, h);
	but2.setSelected(true);
	return;
  }

  private void initToolPanel() {
    // for non model buttons, add to a different Panel
    // the tool buttons have no mouseHandler
    but11 = new LCWToolButton();

    toolPanel = new JPanel();
    Dimension d = new Dimension(MiniMapPanel._setMiniMapWidth,
    numOftoolButtonRow*(LCWButton._buttonSize+_dividerSize)+_dividerSize);
    toolPanel.setPreferredSize(d);
    toolPanel.setMinimumSize(d);
    toolPanel.setMaximumSize(d);
    toolPanel.setLayout(new FlowLayout(FlowLayout.CENTER, _dividerSize,_dividerSize));
    toolPanel.add(but11);
    
    recordBut = new LCWToolButton();
	loadBut = new LCWToolButton();
	playBut = new LCWToolButton();
	stopBut = new LCWToolButton();
	
	toolPanel.add(recordBut);
	toolPanel.add(loadBut);
	toolPanel.add(stopBut);
	toolPanel.add(playBut);
    return;
  }


  public void addMiniMapPanel( MiniMapPanel mp ) {
    miniMap = mp;
    this.add(mp);
  }
  
  public static ProfileCardPanel getProfilePanel() {
  	return ( profilePanel );
  }
  
  public static CardLayout getProfilePanelCard() {
  	return ( profilePanel.getProfilePanelCard() );
  }
}